package org.iniciantestotais.tutorial;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JLayeredPane;

public class Main extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField txtBiblioteca;
	private JPanel panelBiblioteca;
	ArrayList<Biblioteca> ListaBibliotecas = new ArrayList<Biblioteca>();
	private JTable tableBiblioteca;
	JScrollPane scrollPane;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		
		try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
		
		
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main frame = new Main();
					frame.setVisible(true);
					//new Main().setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}


	

	public Main() {
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 858, 607);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu Menu = new JMenu("Menu");
		menuBar.add(Menu);
		
		JMenuItem mntmPrincipal = new JMenuItem("Principal");
		Menu.add(mntmPrincipal);
		mntmPrincipal.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				
				Biblioteca testeBiblioteca = new Biblioteca("UFPB");
				Livro l1 = new Livro("O principe e o pobre");
				l1.setAutor("Mark Twain");
				Livro l2 = new Livro("O livro dos cinco elementos");
				l2.setAutor("Myamoto Musashi");

				Pessoa p1 = new Pessoa();
				p1.setNome("Lisa Simpsn");

				Pessoa p2 = new Pessoa();
				p2.setNome("Bart Simpson");

				testeBiblioteca.addLivro(l1);
				testeBiblioteca.addLivro(l2);
				testeBiblioteca.addPessoa(p1);
				testeBiblioteca.addPessoa(p2);

				System.out.println("Criada a nova biblioteca");
				testeBiblioteca.imprimeStatus();

				testeBiblioteca.emprestimo(l1, p1);
				System.out.println("Emprestado O principe e o pobre para Lisa Simpson");
				testeBiblioteca.imprimeStatus();

				System.out.println("Faz mais alguma coisa");

				testeBiblioteca.devolucao(l1);
				testeBiblioteca.emprestimo(l2, p2);
				testeBiblioteca.imprimeStatus();
				
			}
		});
		
	
		
		
		 JMenuItem mntmCadastrarBiblioteca = new JMenuItem("Cadastrar Biblioteca");
		mntmCadastrarBiblioteca.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			
				
				panelBiblioteca.setVisible(true);
				mostraBibliotecas(ListaBibliotecas);
		
				
			}
		});
		Menu.add(mntmCadastrarBiblioteca);
		
		JMenuItem mntmCadastrarPessoa = new JMenuItem("Cadastrar Pessoa");
		mntmCadastrarPessoa.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
		
				
			}
		});
		Menu.add(mntmCadastrarPessoa);
		
		

		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		

		panelBiblioteca = new JPanel();
		panelBiblioteca.setVisible(false);
		
		JButton btnCadastrarBiblioteca = new JButton("Cadastrar");
		btnCadastrarBiblioteca.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				String aux = txtBiblioteca.getText();
				
				ListaBibliotecas.add(new Biblioteca(aux));
			
				System.out.println(	"Biblioteca Cadastrada");
				
				mostraBibliotecas(ListaBibliotecas);
				
				//for(Biblioteca umaBiblioteca : ListaBibliotecas )
				//System.out.println(umaBiblioteca);
			}
		});
		
		txtBiblioteca = new JTextField();
		txtBiblioteca.setColumns(10);
		
		JLabel lblBiblioteca = new JLabel("Nome da biblioteca");
		scrollPane = new JScrollPane();
		
		
		tableBiblioteca = new JTable();
		tableBiblioteca.setEnabled(false);
		scrollPane.setViewportView(tableBiblioteca);
		tableBiblioteca.setModel(new DefaultTableModel(
				new Object [][] { {null, null} },
	            new String [] {
	                "Index", "Biblioteca"
	            }
	        ) {
	            Class[] types = new Class [] {
	                java.lang.Integer.class, java.lang.String.class
	            };

	            public Class getColumnClass(int columnIndex) {
	                return types [columnIndex];
	            }
	        });
		
		tableBiblioteca.getColumnModel().getColumn(0).setMinWidth(100);
		tableBiblioteca.getColumnModel().getColumn(0).setPreferredWidth(100);
		tableBiblioteca.getColumnModel().getColumn(0).setMaxWidth(100);
		
		JLabel lblNewLabel = new JLabel("Bibliotecas Cadastradas");
		GroupLayout gl_panelBiblioteca = new GroupLayout(panelBiblioteca);
		gl_panelBiblioteca.setHorizontalGroup(
			gl_panelBiblioteca.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelBiblioteca.createSequentialGroup()
					.addGroup(gl_panelBiblioteca.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panelBiblioteca.createSequentialGroup()
							.addContainerGap()
							.addComponent(lblBiblioteca)
							.addGap(18)
							.addComponent(txtBiblioteca, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(btnCadastrarBiblioteca))
						.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 523, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_panelBiblioteca.createSequentialGroup()
							.addGap(152)
							.addComponent(lblNewLabel)))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		gl_panelBiblioteca.setVerticalGroup(
			gl_panelBiblioteca.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelBiblioteca.createSequentialGroup()
					.addGap(29)
					.addGroup(gl_panelBiblioteca.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblBiblioteca)
						.addComponent(txtBiblioteca, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnCadastrarBiblioteca))
					.addGap(84)
					.addComponent(lblNewLabel)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 104, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(233, Short.MAX_VALUE))
		);
		panelBiblioteca.setLayout(gl_panelBiblioteca);
		
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(panelBiblioteca, GroupLayout.PREFERRED_SIZE, 533, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(299, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(panelBiblioteca, GroupLayout.PREFERRED_SIZE, 505, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(391, Short.MAX_VALUE))
		);
		contentPane.setLayout(gl_contentPane);
	}
	
	private void mostraBibliotecas(ArrayList<Biblioteca> Bibliotecas) {
	     
        DefaultTableModel model = (DefaultTableModel) this.tableBiblioteca.getModel();
           model.setNumRows(0);
              
           int i=0;
           for (Biblioteca umaBiblioteca : Bibliotecas) {
        	  
              model.addRow(new Object[]{i++,umaBiblioteca.getNome()}); 
                 
               
           }
           
       
   }
}
